﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Sites 的摘要说明
/// </summary>
[Serializable]
public class Site
{
    #region 成员变量

    private string _urls;

    public string urls
    {
        get { return _urls; }
        set { _urls = value; }
    }

    public long ID;
    public long ParentID;
    public string ParentShopNO;
    public string ParentShopName;
    public long CustomerID;
    public string CustomerNo;
    public string CustomerName;
    public short ShopTypeID;
    public string ShopNo;
    public string ShopName;
    public string Tel;
    public string Mobile;
    public string QQ;
    public string Email;
    public string PostCode;
    public int CityID;
    public string Province;
    public string City;
    public string Address;
    public string Fax;
    public string Other;
    public string BankNameDetail;
    public string BankName;
    public string BankCustName;
    public string BankCardNo;
    public long RegisterOperID;
    public string RegisterCustomerNO;
    public string RegisterCustomerName;
    public DateTime RegisterDate;
    public DateTime VerfyDate;
    public long VerfyOperatorID;
    public string VerfyCustomerNo;
    public string VerfyCustomerName;
    public short Status;
    public string State;
    public string Remark;
    public string SiteTitle;
    public string SiteURL;
    public short SiteStatus;
    public string SiteQQ;
    public string SiteTelephone;
    public string CopyRightInfo;
    public string ICPCert;
    public double PV;
    public double BV;
    public string EnterpriseWebSite;
    public short EnterpriseWebStatus;

    public string Url;

    public SiteOptions SiteOptions;

    #endregion

    #region 索引器

    public Site this[long siteid]
    {
        get
        {
            Site site = new Site();

            site.ID = siteid;

            string ReturnDescription = "";

            if (site.GetSiteInformationByID(ref ReturnDescription) < 0)
            {
                return null;
            }

            return site;
        }
    }

    public Site this[string url]
    {
        get
        {
            string SystemPreFix = Shove._Web.WebConfig.GetAppSettingsString("SystemPreFix") + "Site_";
            Site site = null;

            try
            {
                site = (Site)System.Web.HttpContext.Current.Application[SystemPreFix + url];
            }
            catch { }

            if (site != null)
            {
                return site;
            }

            site = new Site();

            site.Url = url;
            string ReturnDescription = "";

            if (site.GetSiteInformationByUrl(ref ReturnDescription) < 0)
            {
                return null;
            }

            try
            {
                System.Web.HttpContext.Current.Application.Lock();
                System.Web.HttpContext.Current.Application.Add(SystemPreFix + url, site);
            }
            catch { }
            finally
            {
                try
                {
                    System.Web.HttpContext.Current.Application.UnLock();
                }
                catch { }
            }

            return site;
        }
    }

    #endregion

    public Site()
    {
        SiteOptions = new SiteOptions(this);

        _urls = "";

        ID= 1;
        ParentID = -1;
        ParentShopNO= "";
        ParentShopName= "";
        CustomerID= -1;
        CustomerNo= "";
        CustomerName= "";
        ShopTypeID= -1;
        ShopNo= "";
        ShopName= "";
        Tel= "";
        Mobile= "";
        QQ= "";
        Email= "";
        PostCode= "";
        CityID= -1;
        Province= "";
        City= "";
        Address= "";
        Fax= "";
        Other= "";
        BankNameDetail= "";
        BankName= "";
        BankCustName= "";
        BankCardNo= "";
        RegisterOperID = -1;
        RegisterCustomerNO= "";
        RegisterCustomerName= "";
        RegisterDate= DateTime.Now;
        VerfyDate= DateTime.Now;
        VerfyOperatorID= -1;
        VerfyCustomerNo= "";
        VerfyCustomerName= "";
        Status= -1;
        State= "";
        Remark= "";
        SiteTitle= "";
        SiteURL= "";
        SiteStatus= -1;
        SiteQQ= "";
        SiteTelephone= "";
        CopyRightInfo= "";
        ICPCert= "";
        PV= 0;
        BV= 0;
        EnterpriseWebSite= "";
        EnterpriseWebStatus= -1;

        Url = "";
    }

    public Site(long siteid)
    {
        SiteOptions = new SiteOptions(this);

        ID = siteid;    //tbl_Shop 表的 ID

        _urls = "";

        ID = -1;
        ParentID = -1;
        ParentShopNO = "";
        ParentShopName = "";
        CustomerID = -1;
        CustomerNo = "";
        CustomerName = "";
        ShopTypeID = -1;
        ShopNo = "";
        ShopName = "";
        Tel = "";
        Mobile = "";
        QQ = "";
        Email = "";
        PostCode = "";
        CityID = -1;
        Province = "";
        City = "";
        Address = "";
        Fax = "";
        Other = "";
        BankNameDetail = "";
        BankName = "";
        BankCustName = "";
        BankCardNo = "";
        RegisterOperID = -1;
        RegisterCustomerNO = "";
        RegisterCustomerName = "";
        RegisterDate = DateTime.Now;
        VerfyDate = DateTime.Now;
        VerfyOperatorID = -1;
        VerfyCustomerNo = "";
        VerfyCustomerName = "";
        Status = -1;
        State = "";
        Remark = "";
        SiteTitle = "";
        SiteURL = "";
        SiteStatus = -1;
        SiteQQ = "";
        SiteTelephone = "";
        CopyRightInfo = "";
        ICPCert = "";
        PV = 0;
        BV = 0;
        EnterpriseWebSite = "";
        EnterpriseWebStatus = -1;

        Url = "";
    }

    public int Add(ref string ReturnDescription)
    {
        if (ParentID < 0)
        {
            throw new Exception("Sites 尚未初始化 ParentID、CustomerID 变量，无法增加数据");
        }

        ReturnDescription = "";

        int ReturnValue = 0;

        if (ReturnValue < 0)
        {
            ReturnDescription = "数据库读写错误";

            return -1;
        }

        if (ID < 0)
        {
            return (int)ID;
        }

        return 0;
    }

    public int EditByID(ref string ReturnDescription)
    {
        if ((ID < 0) || ((ParentID < 0) && (ID != 1)) || (CustomerID < 0))
        {
            throw new Exception("Sites 尚未初始化到具体的数据实例上，请先使用 GetSiteInformation 等获取数据信息");
        }

        int ReturnValue = 0;
        ReturnDescription = "";

        int Result = 0;

        if (Result < 0)
        {
            ReturnDescription = "数据库读写错误";

            return -1;
        }

        string SystemPreFix = Shove._Web.WebConfig.GetAppSettingsString("SystemPreFix") + "Site_";

        try
        {
            System.Web.HttpContext.Current.Application.Lock();

            foreach (string url in this.SiteURL.Split(','))
            {
                System.Web.HttpContext.Current.Application.Remove(SystemPreFix + url);
            }
        }
        catch { }
        finally
        {
            try
            {
                System.Web.HttpContext.Current.Application.UnLock();
            }
            catch { }
        }

        return ReturnValue;
    }

    public int GetSiteInformationByID(ref string ReturnDescription)
    {
        if (ID < 0)
        {
            throw new Exception("Sites 尚未初始化 ID 变量，无法获取信息");
        }

        int ReturnValue = 0;
        ReturnDescription = "";

        Site ts = new Site();
        Clone(ts);

        int Result = DAL.Procedures.P_GetSiteInformationByID(ID, ref ParentID, ref ParentShopNO, ref ParentShopName, ref CustomerID, ref CustomerNo, ref CustomerName, ref ShopTypeID, ref ShopNo, ref ShopName, ref Tel, ref Mobile, ref QQ, ref Email, ref PostCode, ref CityID, ref Province, ref City, ref Address, ref Fax, ref Other, ref BankNameDetail, ref BankName, ref BankCustName, ref BankCardNo, ref RegisterOperID, ref RegisterCustomerNO, ref RegisterCustomerName, ref RegisterDate, ref VerfyDate, ref VerfyOperatorID, ref VerfyCustomerNo, ref VerfyCustomerName, ref Status, ref State, ref Remark, ref SiteTitle, ref SiteURL, ref SiteStatus, ref SiteQQ, ref SiteTelephone, ref CopyRightInfo, ref ICPCert, ref EnterpriseWebSite, ref EnterpriseWebStatus, ref PV, ref BV, ref ReturnDescription);
        
        if (Result < 0)
        {
            ts.Clone(this);

            ReturnDescription = "数据库读写错误";

            return -1;
        }

        if (ReturnValue < 0)
        {
            ts.Clone(this);

            return ReturnValue;
        }

        Url = _urls;

        return 0;
    }

    public int GetSiteInformationByUrl(ref string ReturnDescription)
    {
        if ((Url == null) || (Url == ""))
        {
            throw new Exception("Sites 尚未初始化 Url 变量，无法获取信息");
        }

        int ReturnValue = 0;
        ReturnDescription = "";

        Site ts = new Site();
        Clone(ts);

        int Result = DAL.Procedures.P_GetSiteInformationByUrl(Url, ref ID, ref ParentID, ref ParentShopNO, ref ParentShopName, ref CustomerID, ref CustomerNo, ref CustomerName, ref ShopTypeID, ref ShopNo, ref ShopName, ref Tel, ref Mobile, ref QQ, ref Email, ref PostCode, ref CityID, ref Province, ref City, ref Address, ref Fax, ref Other, ref BankNameDetail, ref BankName, ref BankCustName, ref BankCardNo, ref RegisterOperID, ref RegisterCustomerNO, ref RegisterCustomerName, ref RegisterDate, ref VerfyDate, ref VerfyOperatorID, ref VerfyCustomerNo, ref VerfyCustomerName, ref Status, ref State, ref Remark, ref SiteTitle, ref SiteURL, ref SiteStatus, ref SiteQQ, ref SiteTelephone, ref CopyRightInfo, ref ICPCert, ref EnterpriseWebSite, ref EnterpriseWebStatus, ref PV, ref BV, ref ReturnDescription);

        if (Result < 0)
        {
            ts.Clone(this);

            ReturnDescription = "数据库读写错误";

            return -1;
        }

        if (ReturnValue < 0)
        {
            ts.Clone(this);

            return ReturnValue;
        }

        Url = _urls;

        return 0;
    }

    public void Clone(Site site)
    {
        site._urls = _urls;

        site.ID = ID;
        site.ParentID = ParentID;
        site.ParentShopNO = ParentShopNO;
        site.ParentShopName = ParentShopName;
        site.CustomerID = CustomerID;
        site.CustomerNo = CustomerNo;
        site.CustomerName = CustomerName;
        site.ShopTypeID = ShopTypeID;
        site.ShopNo = ShopNo;
        site.ShopName = ShopName;
        site.Tel = Tel;
        site.Mobile = Mobile;
        site.QQ = QQ;
        site.Email = Email;
        site.PostCode = PostCode;
        site.CityID = CityID;
        site.Province = Province;
        site.City = City;
        site.Address = Address;
        site.Fax = Fax;
        site.Other = Other;
        site.BankNameDetail = BankNameDetail;
        site.BankName = BankName;
        site.BankCustName = BankCustName;
        site.BankCardNo = BankCardNo;
        site.RegisterOperID = RegisterOperID;
        site.RegisterCustomerNO = RegisterCustomerNO;
        site.RegisterCustomerName = RegisterCustomerName;
        site.RegisterDate = RegisterDate;
        site.VerfyDate = VerfyDate;
        site.VerfyOperatorID = VerfyOperatorID;
        site.VerfyCustomerNo = VerfyCustomerNo;
        site.VerfyCustomerName = VerfyCustomerName;
        site.Status = Status;
        site.State = State;
        site.Remark = Remark;
        site.SiteTitle = SiteTitle;
        site.SiteURL = SiteURL;
        site.SiteStatus = SiteStatus;
        site.SiteQQ = SiteQQ;
        site.SiteTelephone = SiteTelephone;
        site.CopyRightInfo = CopyRightInfo;
        site.ICPCert = ICPCert;
        site.PV = PV;
        site.BV = BV;
        site.EnterpriseWebSite = EnterpriseWebSite;
        site.EnterpriseWebStatus = EnterpriseWebStatus;

        site.Url = Url;
    }
}